﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkStation.UI.Controls
{
    public class MotionTable
    {
        private List<MotionShape> _shapes;
        private RectangleMotion _tableRect;
        private Rectangle2Motion _tableRect2;
        //private LineMotion _x;
        //private LineMotion _y;
        private TextMotion _text;
        private RectangleMotion _centerRect;
        public string Description { get; set; }
        private double _xPosition;
        public double XPosition
        {
            get { return _xPosition; }
            set { _xPosition = value; }
        }
        private double _yPosition;
        public double YPosition
        {
            get { return _yPosition; }
            set { _yPosition = value; }
        }
        private double _rPosition;
        public double RPosition
        {
            get { return _rPosition; }
            set { _rPosition = value; }
        }

        public MotionTable(string description, List<MotionShape> shapes)
        {
            this.Description = description;
            this._shapes = shapes;
        }
        public void Update(MotionCoordinate xCrd, MotionCoordinate yCrd, MotionCoordinate rCrd, int totalWidth, int totalHeight, int marginLeft, int marginUp)
        {
            _shapes.RemoveAll(s => s.Description == "tableRect");
            _shapes.RemoveAll(s => s.Description == "tableRect2");
            _shapes.RemoveAll(s => s.Description == "centerRect");
            _shapes.RemoveAll(s => s.Description == "textRect");

            double x = marginLeft;
            double y = marginUp;
            double r = 0;
            double r2 = 0;
            bool getX = false;
            bool getY = false;
            bool getR = false;
            if (xCrd != null)
                getX = xCrd.GetBoardPosition(_xPosition, totalWidth, totalHeight, marginLeft, marginUp, xCrd.Direction, out x, out r2);
            if (yCrd != null)
                getY = yCrd.GetBoardPosition(_yPosition, totalWidth, totalHeight, marginLeft, marginUp, yCrd.Direction, out y, out r2);
            if (rCrd != null)
                getR = rCrd.GetBoardPosition(_rPosition, totalWidth, totalHeight, marginLeft, marginUp, rCrd.Direction, out r, out r2);
            if (xCrd != null && yCrd != null)
            {
                if (getX && getY)
                {
                    _tableRect = new RectangleMotion("tableRect");
                    _tableRect.Alpha = 60;
                    _tableRect.LineWidth = 1;
                    _tableRect.PaintColor = Color.Blue;
                    _tableRect.FillColor = Color.Yellow;
                    _tableRect.IsFill = true;
                    _shapes.Add(_tableRect);
                    _tableRect.StartPoint = new PointF((float)x - 25, (float)y - 10);
                    _tableRect.EndPoint = new PointF((float)x + 25, (float)y + 10);

                    _centerRect = new RectangleMotion("centerRect");
                    _centerRect.Alpha = 60;
                    _centerRect.LineWidth = 1;
                    _centerRect.PaintColor = Color.Orange;
                    _centerRect.FillColor = Color.Coral;
                    _centerRect.IsFill = true;
                    _shapes.Add(_centerRect);
                    _centerRect.StartPoint = new PointF((float)x - 2, (float)y - 2);
                    _centerRect.EndPoint = new PointF((float)x + 2, (float)y + 2);

                    _text = new TextMotion("textRect");
                    _text.LineWidth = 2;
                    _text.PaintColor = Color.Black;
                    _text.Text = $"({_xPosition.ToString("0.000")},{_yPosition.ToString("0.000")})";
                    _text.FontName = "Arial";
                    _text.FontSize = 10.5f;
                    _text.StartPoint = new PointF((float)x + 8, (float)y + 8);
                    _shapes.Add(_text);
                }
            }
            else if(xCrd != null || yCrd != null)
            {
                if (getX || getY)
                {
                    if (getX)
                    {
                        _tableRect = new RectangleMotion("tableRect");
                        _tableRect.Alpha = 60;
                        _tableRect.LineWidth = 1;
                        _tableRect.PaintColor = Color.Blue;
                        _tableRect.FillColor = Color.Yellow;
                        _tableRect.IsFill = true;
                        _shapes.Add(_tableRect);
                        _tableRect.StartPoint = new PointF((float)x - 10, (float)y - 10);
                        _tableRect.EndPoint = new PointF((float)x + 10, (float)y + 10);
                    }
                    if (getY)
                    {
                        _tableRect = new RectangleMotion("tableRect");
                        _tableRect.Alpha = 60;
                        _tableRect.LineWidth = 1;
                        _tableRect.PaintColor = Color.Blue;
                        _tableRect.FillColor = Color.Yellow;
                        _tableRect.IsFill = true;
                        _shapes.Add(_tableRect);
                        _tableRect.StartPoint = new PointF((float)x - 10, (float)y - 10);
                        _tableRect.EndPoint = new PointF((float)x + 10, (float)y + 10);
                    }
                }
                if (getX || getY)
                {
                    _centerRect = new RectangleMotion("centerRect");
                    _centerRect.Alpha = 60;
                    _centerRect.LineWidth = 1;
                    _centerRect.PaintColor = Color.Orange;
                    _centerRect.FillColor = Color.Coral;
                    _centerRect.IsFill = true;
                    _shapes.Add(_centerRect);
                    _centerRect.StartPoint = new PointF((float)x - 2, (float)y - 2);
                    _centerRect.EndPoint = new PointF((float)x + 2, (float)y + 2);

                    _text = new TextMotion("textRect");
                    _text.LineWidth = 2;
                    _text.PaintColor = Color.Black;
                    _text.Text = getX ? $"{_xPosition.ToString("0.000")}" : $"{_yPosition.ToString("0.000")}";
                    _text.FontName = "Arial";
                    _text.FontSize = 10.5f;
                    _text.StartPoint = new PointF((float)x + 8, (float)y + 8);
                    _shapes.Add(_text);
                }
            }
            else if(rCrd != null)
            {
                x = totalWidth < totalHeight ? (float)(marginLeft + totalWidth / 2) : (float)(marginLeft + totalWidth / 2);
                y = totalWidth < totalHeight ? (float)(marginUp + totalHeight / 2) : (float)(marginUp + totalHeight / 2);

                _tableRect2 = new Rectangle2Motion("tableRect2");
                _tableRect2.Alpha = 60;
                _tableRect2.LineWidth = 1;
                _tableRect2.PaintColor = Color.Blue;
                _tableRect2.FillColor = Color.Yellow;
                _tableRect2.IsFill = true;
                _shapes.Add(_tableRect2);
                _tableRect2.CenterPoint = new PointF((float)r, (float)r2);
                _tableRect2.RotateAngle = (float)_rPosition;
                _tableRect2.Width = 20;
                _tableRect2.Height = 20;

                _centerRect = new RectangleMotion("centerRect");
                _centerRect.Alpha = 60;
                _centerRect.LineWidth = 1;
                _centerRect.PaintColor = Color.Orange;
                _centerRect.FillColor = Color.Coral;
                _centerRect.IsFill = true;
                _shapes.Add(_centerRect);
                _centerRect.StartPoint = new PointF((float)x - 2, (float)y - 2);
                _centerRect.EndPoint = new PointF((float)x + 2, (float)y + 2);

                _text = new TextMotion("textRect");
                _text.LineWidth = 2;
                _text.PaintColor = Color.Black;
                _text.Text = $"{_rPosition.ToString("0.000")}";
                _text.FontName = "Arial";
                _text.FontSize = 10.5f;
                _text.StartPoint = new PointF((float)r + 5, (float)r2 - 20);
                _shapes.Add(_text);
            }
        }
    }
}
